package com.icebergteam.warfacewidget.core;

import com.icebergteam.warfacewidget.AppManager;
import com.icebergteam.warfacewidget.WidgetProvider;
import com.icebergteam.warfacewidget.data.sql.VideosTable;
import android.content.ContentValues;
import android.content.Intent;

public class IntentService extends android.app.IntentService{
	public static String className = IntentService.class.getCanonicalName();
	public static String KEY_INTENTSERVICE = String.format("%s.%s", className, "KEY_INTENTSERVICE");
	public static String UPDATE_VIDEOS_READ = String.format("%s.%s", className,"UPDATE_VIDEOS_READ");

	public IntentService() {
		super(className);
		
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		try{
			if(intent.getExtras().get(KEY_INTENTSERVICE) != null && intent.getExtras().get(KEY_INTENTSERVICE).equals(UPDATE_VIDEOS_READ)){
				String url = intent.getStringExtra(VideosTable.FIELD_URL);
				Boolean read = intent.getBooleanExtra(VideosTable.FIELD_READ, true);
				
				ContentValues dataToInsert = new ContentValues();    
				dataToInsert.put(VideosTable.FIELD_URL, url);
				dataToInsert.put(VideosTable.FIELD_READ, read);
				
				AppManager.getDatabase().getVideosTable().updateOrInsert(dataToInsert);
				
				WidgetProvider.refreshWidget(intent);
				
				LogManager.LogVA(className, intent, UPDATE_VIDEOS_READ, url, read);
			}
		}
		catch(Exception e){
			e.printStackTrace();
			LogManager.LogEA(className, e.getLocalizedMessage());
		}
	
		
	}

	@Override
	public void onDestroy() {
		LogManager.LogVA(className);
		super.onDestroy();
	}

}
